/**
 *
 * author ThuanLT
 * Aug 20, 2014 11:45:47 PM
 *
 */
package vn.fsoft.gst.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import vn.fsoft.gst.domain.Town;

@Repository
@SuppressWarnings({"unchecked"})
public class TownDAOImpl implements TownDAO {
	
	@Autowired 
	private SessionFactory sessionFactory;

	public int addTown(Town townName) {
		Session session = sessionFactory.getCurrentSession();
		return (Integer) session.save(townName);
	}
	
	public void updateTown(Town town){
		Session session = sessionFactory.getCurrentSession();
		session.merge(town);
		
	}
	
	public void deleteTown(int townID){
		Session session = sessionFactory.getCurrentSession();
		Town town = getTownByID(townID);
		session.delete(town);
	}
	
	public List<Town> getListTown(){
		Session session = sessionFactory.getCurrentSession();
		List<Town> list = session.createQuery("from town").list();
		return list;
	}
	
	public Town getTownByID(int townID){
		Session session = sessionFactory.getCurrentSession();
		return (Town) session.get(Town.class, townID);		
	}
}
